package com.glasgowrax.hack.domain.directions;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.glasgowrax.hack.domain.LatLng;

public class Step {
	
	public static final int MODE_OTHER = -1;
	public static final int MODE_WALKING = 0;
	public static final int MODE_BUS = 1;
	public static final int MODE_TRAIN = 2;
	public static final int MODE_SUBWAY = 3;
	
	private String instructions;
	private String polyline;
	private String travelMode;
	
	private double distanceMeters;
	private String distanceDescription;
	
	private long durationSeconds;
	private String durationDescription;
	
	private LatLng endPoint;	
	private LatLng startPoint;
	
	private List<Step> substeps;
	private TransitDetails transitDetails;
	
	private int mode;
	
	public Step(JSONObject json) {
		
		try {
			JSONObject polylinePoints = (json.has("polyline")) ? json.getJSONObject("polyline") : new JSONObject();
			JSONObject distance = (json.has("distance")) ? json.getJSONObject("distance") : new JSONObject();
			JSONObject duration = (json.has("duration")) ? json.getJSONObject("duration") : new JSONObject();
			JSONObject end = (json.has("end_location")) ? json.getJSONObject("end_location") : new JSONObject();
			JSONObject start = (json.has("start_location")) ? json.getJSONObject("start_location") : new JSONObject();
			JSONArray steps = (json.has("steps")) ? json.getJSONArray("steps") : new JSONArray();
			
			instructions = (json.has("html_instructions")) ? json.getString("html_instructions") : "";
			polyline = (polylinePoints.has("points")) ? polylinePoints.getString("points") : "";
			travelMode = (json.has("travel_mode")) ? json.getString("travel_mode") : "";
			
			distanceMeters = (distance.has("value")) ? distance.getLong("value") : -1;
			distanceDescription = (distance.has("text")) ? (String) distance.get("text") : "";

			durationSeconds = (duration.has("value")) ? duration.getLong("value") : -1;
			durationDescription = (duration.has("text")) ? (String) duration.get("text") : "";
			
			endPoint = (end.has("lat") && end.has("lng")) ? new LatLng(end.getDouble("lat"), end.getDouble("lng")) : null;
			startPoint = (start.has("lat") && start.has("lng")) ? new LatLng(start.getDouble("lat"), start.getDouble("lng")) : null;
			
			substeps = new ArrayList<Step>();
			for (int i=0; i<steps.length(); i++) {
				substeps.add(new Step(steps.getJSONObject(i)));
			}
			
			transitDetails = ((json.has("transit_details")) ? new TransitDetails(json.getJSONObject("transit_details")) : null);
			
			setMode();
		
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		
	}
	
	private void setMode() {
		if (travelMode != null && travelMode.equalsIgnoreCase("WALKING")) mode = MODE_WALKING;
		else if (transitDetails != null && transitDetails.getLine().getVehicle().getName().equalsIgnoreCase("BUS")) mode = MODE_BUS;
		else if (transitDetails != null && transitDetails.getLine().getVehicle().getName().equalsIgnoreCase("SUBWAY")) mode = MODE_SUBWAY;
		else if (transitDetails != null && transitDetails.getLine().getVehicle().getName().equalsIgnoreCase("TRAIN")) mode = MODE_TRAIN;
		else mode = MODE_OTHER;
	}

	public String getInstructions() {
		return instructions;
	}

	public void setInstructions(String instructions) {
		this.instructions = instructions;
	}

	public String getPolyline() {
		return polyline;
	}

	public void setPolyline(String polyline) {
		this.polyline = polyline;
	}

	public String getTravelMode() {
		return travelMode;
	}

	public void setTravelMode(String travelMode) {
		this.travelMode = travelMode;
	}

	public double getDistanceMeters() {
		return distanceMeters;
	}

	public void setDistanceMeters(double distanceMeters) {
		this.distanceMeters = distanceMeters;
	}

	public String getDistanceDescription() {
		return distanceDescription;
	}

	public void setDistanceDescription(String distanceDescription) {
		this.distanceDescription = distanceDescription;
	}

	public long getDurationSeconds() {
		return durationSeconds;
	}

	public void setDurationSeconds(long durationSeconds) {
		this.durationSeconds = durationSeconds;
	}

	public String getDurationDescription() {
		return durationDescription;
	}

	public void setDurationDescription(String durationDescription) {
		this.durationDescription = durationDescription;
	}

	public LatLng getEndPoint() {
		return endPoint;
	}

	public void setEndPoint(LatLng endPoint) {
		this.endPoint = endPoint;
	}

	public LatLng getStartPoint() {
		return startPoint;
	}

	public void setStartPoint(LatLng startPoint) {
		this.startPoint = startPoint;
	}

	public List<Step> getSubsteps() {
		return substeps;
	}

	public void setSubsteps(List<Step> substeps) {
		this.substeps = substeps;
	}

	public TransitDetails getTransitDetails() {
		return transitDetails;
	}

	public void setTransitDetails(TransitDetails transitDetails) {
		this.transitDetails = transitDetails;
	}

	public int getMode() {
		return mode;
	}

	public void setMode(int mode) {
		this.mode = mode;
	}
	
	
	
}
